package com.farina.cryptobox;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.UnsupportedEncodingException;
import java.io.FileWriter;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;

import com.farina.cryptobox.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class NewAccount extends Activity {
	
	static final int DIALOG_CREATE_ACCOUNT_ID = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_account);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_new_account, menu);
		return true;
	}

	public int writeNewAccount(String usn, String pwd){
		File f = new File(getExternalFilesDir(null), "accounts.txt");
		FileWriter fw = null;
		BufferedWriter bw = null;
		try{
			fw = new FileWriter(f, true);
			bw = new BufferedWriter(fw);
			bw.append(usn);
			bw.append(pwd);
			bw.close(); 
		}
		catch (IOException e) {      
			e.printStackTrace();
			return 3;
		}
		return 0;
	}
	
	public boolean checkAccount(String usn){
		File f = new File(getExternalFilesDir(null), "accounts.txt");
		
		FileReader fr = null;
		BufferedReader br = null;
		try{
			fr = new FileReader(f);
			br = new BufferedReader(fr);
			while(true){
				String tmpUsn = br.readLine();
				if(tmpUsn==null){br.close(); return false;}
				br.readLine();
				if(tmpUsn.equals(usn))break;
			}
			
			br.close(); 
		}
		catch (FileNotFoundException e) {      
			return false; 
		}
		catch (IOException e) {      
			e.printStackTrace(); 
		}
		
		return true;
	}

	public void createAccountReal() throws NoSuchAlgorithmException, UnsupportedEncodingException {
		EditText usnText = (EditText) findViewById(R.id.usnNewAccField);
		String usn = usnText.getText().toString();
		EditText pwdText = (EditText) findViewById(R.id.pwdNewAccField);
		String pwd = pwdText.getText().toString();

		pwd=CryptoFunction.SHA256(pwd)+"\n";
//		usn=usn+"\n";
		try {
			usn=CryptoFunction.MD5(usn)+"\n";
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		int res=writeNewAccount(usn, pwd);
		if(res==3)return;

		setResult(RESULT_OK);
		this.finish();


	}
	
	public void createAccount(View view){
		
		EditText usnText = (EditText) findViewById(R.id.usnNewAccField);
		String usn = usnText.getText().toString();
		EditText pwdText = (EditText) findViewById(R.id.pwdNewAccField);
		String pwd = pwdText.getText().toString();
		EditText pwdReText = (EditText) findViewById(R.id.retypeNewAccField);
		String pwdRe = pwdReText.getText().toString();

		try {
			usn=CryptoFunction.MD5(usn);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		if(usn.equals("") || checkAccount(usn)){
			TextView usnLabel=(TextView) findViewById(R.id.usnNewAccLabel);
			usnLabel.setText("Not Valid Username!");
			usnLabel.setTextColor(Color.RED);
			return;
		}

		if(pwd.length()<2){
			TextView pwdLabel=(TextView) findViewById(R.id.pwdNewAccLabel);
			pwdLabel.setText("Too Short Password! (almost 8 char)");
			pwdLabel.setTextColor(Color.RED);
			return;
		}

		if(!pwdRe.equals(pwd)) {
			TextView retpwdLabel=(TextView) findViewById(R.id.retypeNewAccLabel);
			retpwdLabel.setText("Please Retype the Password");
			retpwdLabel.setTextColor(Color.RED);
			pwdReText.setText("");
			return;
		}
		
		this.showDialog(DIALOG_CREATE_ACCOUNT_ID);
		
	}
	
	protected Dialog onCreateDialog(int id) {
		Dialog dialog=null;
		switch(id) {
		case DIALOG_CREATE_ACCOUNT_ID:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("Are you sure you want to create a new Account?")
			.setCancelable(false)
			.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					try {
						createAccountReal();
					} catch (NoSuchAlgorithmException e) {
						e.printStackTrace();
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					}
				}
			})
			.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					dialog.cancel();
				}
			});
			dialog = builder.create();
			break;
		default:
			dialog = null;
		}
		return dialog;
	}

	public void close(View view) {
		this.finish();
	}


}
